import { Injectable } from '@angular/core';

import {HttpService} from '../shared/http/http.service';
import {ConfigProvider} from '../providers/config.provider';

import {SlideshowsModel, SlideshowsCategoryModel } from '../models/SlideshowsModel';

/*
* 帐号服务
*/
@Injectable()
export class SlideshowsService {

    constructor(
        private httpService: HttpService
    ) {

    }

    public updateSlideshowsCategory(categoryId, categoryName, callback) {
        const url = ConfigProvider.apiBaseURL + 'cms/slideshows/categories/update';
        this.httpService.post(url, {categoryId: categoryId, categoryName: categoryName},
            function (successful, data, res) {
              if (successful && callback) {
                  callback(true);
              }
            }, function (successful, msg, err) {
                if (callback) {
                    callback(false, err);
                }
            });
    }

    public removeSlideshowsCategory(categoryId, callback) {
        const url = ConfigProvider.apiBaseURL + 'cms/slideshows/categories/remove';
        this.httpService.post(url, {categoryId: categoryId},
            function (successful, data, res) {
              if (successful && callback) {
                  callback(true);
              }
            }, function (successful, msg, err) {
                if (callback) {
                    callback(false, err);
                }
            });
    }

    public getSlideshows(categoryId, callback) {
        const url = ConfigProvider.apiBaseURL + 'cms/slideshows';
        this.httpService.post(url, {categoryId: categoryId},
        function (successful, data, res) {
          if (successful && callback) {
              const result = (<Array<SlideshowsModel>>data.result);
              callback(true, result);
          }
        }, function (successful, msg, err) {
            if (callback) {
                callback(false, err);
            }
        });
    }

    public getSlideshowsCategories(callback) {
        const url = ConfigProvider.apiBaseURL + `cms/slideshows/categories`;
        this.httpService.get(url, null,
        function (successful, data, res) {
          if (successful && callback) {
              const result = (<Array<SlideshowsCategoryModel>>data.result);
              callback(true, result);
          }
        }, function (successful, msg, err) {
            if (callback) {
                callback(false, err);
            }
        });
    }

    public addSlideshow(categoryId, imageUrl, callback) {
        const url = ConfigProvider.apiBaseURL + `cms/slideshows/add`;
        this.httpService.post(url, {categoryId: categoryId, imageUrl: imageUrl},
        function (successful, data, res) {
          if (successful && callback) {
              callback(true);
          }
        }, function (successful, msg, err) {
            if (callback) {
                callback(false, err);
            }
        });
    }

    public updateSlideshow(categoryId, slideshowId, title, callback) {
        const url = ConfigProvider.apiBaseURL + `cms/slideshows/update`;
        this.httpService.post(url, {categoryId: categoryId, slideshowId: slideshowId, title: title},
        function (successful, data, res) {
          if (successful && callback) {
              callback(true);
          }
        }, function (successful, msg, err) {
            if (callback) {
                callback(false, err);
            }
        });
    }

    public removeSlideshow(categoryId, slideshowId, callback) {
        const url = ConfigProvider.apiBaseURL + `cms/slideshows/remove`;
        this.httpService.post(url, {categoryId: categoryId, slideshowId: slideshowId},
        function (successful, data, res) {
          if (successful && callback) {
              callback(true);
          }
        }, function (successful, msg, err) {
            if (callback) {
                callback(false, err);
            }
        });
    }
}
